<?php

namespace app\admin\controller;

/**
 * 一物一码
 */
class newcode extends base
{
    //覆盖默认
    public function _initialize()
    {

    }

    /**
     * 红包列表
     */
    public function newcode()
    {
        $where = array();
        $getdata = $this->request->param();
        if (isset($getdata['state'])) {
            $where['state'] = $getdata['state'];
        }
        $data = db('newcode')->order('id DESC')->where($where)->paginate(3);
        $this->assign('data', $data);
        return $this->fetch();

    }

    /**
     * 查看红包二维码
     */
    public function checkallcode()
    {
        $data = db('newcode')->field('ticket')->order('id DESC')->where('state', 0)->paginate();

        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 增加二维码
     *
     * @return \think\response\View
     */
    public function newcodeadd()
    {

        if ($this->request->isPost()) {
            $qrModel = db('newcode');
            $IN = input();

            for ($i = 0; $i < $IN['num']; $i++) {
                $addredbao[$i] =
                    [
                        'ticket' => substr(md5(time()+$i),0,10),
                        'title' => $IN['title'],
                        'subtitle' => $IN['subtitle'],
                        'money' => $IN['money'],
                        'textmessage' => $IN['textmessage'],
                        'state' => 0,
                        'logo_img' => $IN['logo_img'],
                        'bg_img' => $IN['bg_img'],
                        'create_time' => time(),
                    ];
            }
            if ($qrModel->insertAll($addredbao)) {
                ajaxReturn(['url' => url('newcode', ['type' => 'list'])], 1, '增加二维码成功');
            }


        } else {
            return $this->fetch();
        }
    }

    public function delnewcode($id)
    {
        if ($this->request->isAjax()) {
            db('newcode')->where('id', $id)->delete();
            ajaxMsg('1', '删除成功');
        }
    }
}
